<?php
/**
 * Created by PhpStorm.
 * User: liuhong
 * Date: 2018/11/9
 * Time: 上午11:12
 */

namespace Api\Controller;

use Api\Service\GoodsService;
use Think\Exception;

class GoodsController extends BaseController
{
    //商品详情
    public function goodsDetail(){
        try{
            $goodsId=I('post.goods_id');
            if(empty($goodsId)){
                $this->ajax(202,'商品ID不能为空');
            }
            $goodsInfo= GoodsService::S()->findGoodsInfoByGoodsId($goodsId,GoodsService::S()->goodsFiled());
            $goodsInfo['goods_pic']=C('UPYUN_DOMAIN').$goodsInfo['goods_pic'];//$goodsInfo["grade{$this->member['grade_id']}_price"];
            $thumb=json_decode($goodsInfo['goods_thumb'],true);//$goodsInfo["grade{$this->member['grade_id']}_price"];
            foreach ($thumb as $sk=>$sv){
                $thumb[$sk]=C('UPYUN_DOMAIN').$sv;
            }
            $goodsInfo['goods_thumb']=$thumb;
            $goodsInfo['freight_payer']=$goodsInfo['freight_payer']==1?'包邮':'统一运费';
            $goodsInfo['goods_desc']=str_replace('src="','src="'.get_domain(),$goodsInfo['goods_desc']);
            if($goodsInfo){
                $this->ajax(200,'success',$goodsInfo);
            }else{
                $this->ajax(202,'商品信息不存在');
            }
        }catch (Exception $ex){
            $this->ajaxException($ex,$_POST);
        }

    }


    //查询商品类型
    public function getGoodsList() {
        $page=I('post.page');
        $page_num=10;
        $where=array();
        $grade = I('post.grade');
        if($grade==3) {
            $where = array(
                'is_grade3' => 1
            );
        }elseif($grade==4) {
            $where = array(
                'is_grade4' => 1
            );
        }elseif($grade==2) {
            $where = array(
                'grade_id' => 2
            );
        }
        $filed='goods_name,goods_pic,goods_id';
        $goods_name=I('post.goods_name');
        if(!empty($goods_name)){
            $where['goods_name']=array('like',"%{$goods_name}%");
        }

        $list = GoodsService::S()->queryGoodsListByWhere($where,$page,$page_num,$filed);
        if(!$list) {
            $this->ajax(200,'商品信息为空');
            //return_json(1,'');
        }
        foreach($list as $k=>$v) {
            $list[$k]['goods_pic'] = C('UPYUN_DOMAIN').$v['goods_pic'];
            $list[$k]['num'] = 0;
        }
        $this->ajax(200,'success',$list);
        //return_json(0,'',$list);
    }

    //商品查询
    public function goodsSearch(){
        $page=I('post.page');
        $page_num=10;
        $where=array();
        $grade = I('post.grade');
        if($grade==3) {
            $where = array(
                'is_grade3' => 1
            );
        }elseif($grade==4) {
            $where = array(
                'is_grade4' => 1
            );
        }elseif($grade==2) {
            $where = array(
                'grade_id' => 2
            );
        }
        $where['ischeck']=1;
        $where['stock']=array('EGT',1);
        $where['gtype_id']=array('EGT',1);
        $filed='goods_name,goods_pic,goods_id,gtype_id,shop_price,market_price';
        $goods_name=I('post.goods_name');
        if(!empty($goods_name)){
            $where['goods_name']=array('like',"%{$goods_name}%");
        }

        $orderby = I('post.orderby');
        if($orderby == "pricedown"){
            $order = "shop_price ASC";
        }else if($orderby == "pv"){
            $order = "all_views ASC";
        }else if($orderby == "sales"){
            $order = "sale_all ASC";
        }else if ($orderby=='all'){
            $order = "ord ASC";
        }else{
            $order = "ord ASC";
        }

        $list = GoodsService::S()->queryGoodsListByWhere($where,$page,$page_num,$filed,$order);
        if(!$list) {
            $this->ajax(200,'商品信息为空');
            //return_json(1,'');
        }
        //dump($list);die;
        $gtypeInfo=GoodsService::S()->backGoodsTypeInfo();
        $backArray=array();
        foreach($list as $k=>$v) {
            $list[$k]['goods_pic'] = C('UPYUN_DOMAIN').$v['goods_pic'];
            $list[$k]['num'] = 0;
            $type_name=$gtypeInfo[$v['gtype_id']];
            if(empty($type_name)){
                $type_name='';
            }
            $backArray[$type_name][]=$list[$k];
        }
        $this->ajax(200,'success',$backArray);
    }

    //根据商品类型查询出商品信息
    public function getGoodsByType(){
        $type=I('post.type_id');
        $page=I('post.page');
        $page_num=10;
        $filed='goods_name,goods_pic,goods_id,shop_price,market_price,sale_basic';
        $where=array();
        if(!empty($type)){
            $where['gtype_id']=$type;//array('like',"%{$goods_name}%");
        }
        $goods_name=I('post.type_name');
        if(!empty($goods_name)){
            $where['goods_name']=array('like',"%{$goods_name}%");
        }
        $list = GoodsService::S()->queryGoodsListByWhere($where,$page,$page_num,$filed);
        if(!$list) {
            $this->ajax(200,'商品信息为空');
            //return_json(1,'');
        }
        foreach($list as $k=>$v) {
            $list[$k]['goods_pic'] = C('UPYUN_DOMAIN').$v['goods_pic'];
            $list[$k]['num'] = 0;
        }
        $this->ajax(200,'success',$list);
        //return_json(0,'',$list);
    }

}